<template>
  <div>
    <a-page-header class="pt-0" @back="back" :title="t('填写表单')"></a-page-header>
    <a-spin :spinning="table.loading" class="w-[100%]">
      <a-form
        layout="vertical"
        @finish="submit"
        :rules="formRules"
        :model="form"
        ref="formRef"
        :label-col="{ span: 4 }"
        :wrapper-col="{ span: 8 }"
      >
        <a-card :bordered="false" :body-style="{ paddingTop: '0' }">
          <a-form-item label="分类名称" name="categoryName">
            <a-input autofocus allow-clear v-model:value="form.categoryName" />
          </a-form-item>

          <div class="flex gap-4">
            <a-button type="primary" htmlType="submit">{{ t('保存') }}</a-button>
            <a-button @click="reset">{{ t('重置') }}</a-button>
          </div>
        </a-card>
      </a-form>
    </a-spin>
  </div>
</template>

<script setup lang="ts">
import type { IResponse } from '@/api/config/types';
import { list } from '../data/curd';
import { allowEditMenu, table } from '../data/data';
import { formRef, form, formRules } from '../data/form';
const { t } = useI18n();
import { addCategory } from '@/api/modules/data/classify';
import { notify } from '@/utils/common/common';
import { updateCategory } from '@/api/modules/data/classify';
import type { GoodsCategory } from '@/api/modules/data/classify';

const back = () => {
  allowEditMenu.value = false;
  reset();
};
const submit = async () => {
  let result: IResponse<GoodsCategory>;
  if (!form.value.categoryId) {
    const { data } = await addCategory(form.value);
    result = data;
  } else {
    const { data } = await updateCategory(form.value);
    result = data;
  }
  notify(result);
  await list();
  back();
};
const reset = () => {
  formRef.value?.resetFields();
};
</script>

<style lang="scss" scoped>
:deep(.ant-card) {
  box-shadow: none;
}
</style>
